package savefile;

import java.io.*;
import java.net.Socket;
import java.util.Scanner;

public class SocketFile {

    public static void main(String[] args) {
        Socket socket = null;
        String reader = null;
        OutputStream os = null;
        InputStream is = null;
        OutputStream outputStream = null;
        byte[] buf = new byte[2048];
        int length = 0;
        String path = null;
        File file = null;
        String writer = null;
        Scanner input = new Scanner(System.in);
        BufferedReader br = null;
        try {
            socket = new Socket("localhost",6248);
        } catch (IOException e) {
            e.printStackTrace();
        }
        while (true){
            try {
                path = input.nextLine();
                file = new File(path);
                is = new FileInputStream(file);
                os = socket.getOutputStream();
                //outputStream = socket.getOutputStream();
                buf = path.getBytes();
                os.write(buf);
                os.flush();
                while((length = is.read(buf)) != -1){
                    os.write(buf,0,length);
                    os.flush();
                }
                socket.shutdownOutput();//关闭套接字的输出流
                br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
                while ((reader = br.readLine()) != null) {
                    System.out.println(reader);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (is != null) {
                    try {
                        is.close();
                        is = null;
                        br.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (os != null) {
                    try {
                        os.close();
                        os = null;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
   /* public SocketFile(){
        //BufferedReader br = null;


            *//*br = new BufferedReader(new InputStreamReader(socket.getInputStream(),"UTF-8"));
            while ((reader = br.readLine()) != null){
                System.out.println(reader);
            }*//*

    }*/
   /* class  PrintThread implements Runnable{
        @Override
        public void run() {


        }
    }*/
}
